package com.ld.security.common.mybatis.base.po;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ld.security.common.core.base.LimitPo;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.base.entity.SuperEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.LinkedList;
import java.util.List;

@Data
public class PagePo<E> extends ListPo<E> implements LimitPo {


	@ApiModelProperty("页数")
	private Integer pageNumber = 1;
	
	@ApiModelProperty("页高")
	private Integer pageSize = 15;

	public <V> PagePo<V> newQuery(){
		PagePo po =new PagePo();
		po.setOrderBy(getOrderBy());
		po.setAsc(isAsc());
		po.setPageSize(pageSize);
		po.setPageNumber(pageNumber);
		return po;
	}


	public Page toPage(){
		Page page = new Page();
		page.setCurrent(pageNumber);
		page.setSize(pageSize);

		if(this.getQuery() instanceof SuperEntity){
			if(StringUtil.isNull(getOrderBy())){
				//根据时间逆序
				this.setOrderBy("crtTime");
				this.setAsc(false);
			}
		}

		if(StringUtil.isNotNull(this.getOrderBy())){
			List orders=  new LinkedList<>();
			String order = StringUtil.javaToColumn(this.getOrderBy());
			orders.add(order);
			if(this.isAsc()){
				page.setAscs(orders);
			}else{
				page.setDescs(orders);
			}
		}

		return page;
	}

}
